//
//  OtherSedentaryReminderCell.swift
//  MoyoungDevice
//
//  Created by 易先生 on 2022/8/17.
//

import UIKit
import MYModule

class OtherSedentaryReminderCell: TableViewCell {

	private var cellModel: OtherSedentaryReminderCellModel?

	override func makeUI() {
		super.makeUI()
		contentView.backgroundColor = UIColor.background

		contentView.addSubview(titleLabel)
		contentView.addSubview(descLabel)
		contentView.addSubview(switchView)
		contentView.addSubview(lineView)

		titleLabel.snp.makeConstraints { make in
			make.top.equalToSuperview().offset(16)
			make.leading.equalToSuperview().offset(20)
			make.trailing.equalToSuperview().offset(-54)
		}

		descLabel.snp.makeConstraints { make in
            make.top.equalTo(titleLabel.snp.bottom).offset(5)
			make.leading.equalToSuperview().offset(20)
			make.bottom.equalToSuperview().offset(-16)
			make.trailing.equalToSuperview().offset(-64)
		}

		switchView.snp.makeConstraints { make in
			make.trailing.equalToSuperview().offset(-20)
			make.size.equalTo(CGSize(width: 44, height: 30))
			make.centerY.equalToSuperview()
		}

		lineView.snp.makeConstraints { make in
			make.leading.equalToSuperview().offset(20)
			make.trailing.equalToSuperview().offset(-20)
			make.height.equalTo(1)
			make.bottom.equalToSuperview()
		}
	}

	public func bind(to cellModel: OtherSedentaryReminderCellModel) {
        self.cellModel = cellModel
		titleLabel.text = cellModel.title
		descLabel.text = cellModel.content
		switchView.setOn(isOn: cellModel.isOn)
        switchView.isEnabled = cellModel.isEnabled
        guard "other_low_battery_reminder_title".language == cellModel.title || cellModel.isOn == false else {
            lineView.isHidden = false
            return
        }
        lineView.isHidden = true
	}

	public lazy var titleLabel: Label = {
		let titleLabel = Label()
		titleLabel.textColor = UIColor.assist_222222
		titleLabel.numberOfLines = 0
		titleLabel.font = Fonts.systemFont(ofSize: 16)
		return titleLabel
	}()

	private lazy var descLabel: Label = {
		let descLabel = Label()
		descLabel.textColor = UIColor.assist_A9A9A9
		descLabel.numberOfLines = 0
		descLabel.font = Fonts.systemFont(ofSize: 12)
		return descLabel
	}()

	private lazy var switchView: CustomSwitchView = {
		let switchView = CustomSwitchView()
		switchView.onTintColor = UIColor.theme
		switchView.delegate = self
        switchView.inactiveColor = UIColor.assist_D0D0D0
		return switchView
	}()

	public lazy var lineView: View = {
		let lineView = View()
		lineView.backgroundColor = UIColor.assist_D0D0D0
		return lineView
	}()
}

extension OtherSedentaryReminderCell: CustomSwitchViewDelegate {
	func switchAction(isOn: Bool) {
		self.cellModel?.isOpen.accept(isOn)
		self.cellModel?.isOn = isOn
	}
}
